<template>
  <div>
    <el-card>
      <div slot="header" class="clearfix">
        <el-breadcrumb separator-class="el-icon-arrow-right">
          <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
          <el-breadcrumb-item>评论管理</el-breadcrumb-item>
        </el-breadcrumb>
      </div>
      <el-table :data="articles" style="width: 100%" stripe v-loading="loading">
        <el-table-column prop="title" label="标题" width="width">
        </el-table-column>
        <el-table-column
          prop="total_comment_count"
          label="总评论数"
          width="width"
        >
        </el-table-column>
        <el-table-column
          prop="fans_comment_count"
          label="粉丝评论数"
          width="width"
        >
        </el-table-column>
        <el-table-column prop="comment_status" label="评论状态" width="width">
          <template slot-scope="scope">
            {{ scope.row.comment_status ? '正常' : '关闭' }}
          </template>
        </el-table-column>
        <el-table-column prop="comment_status" label="操作" width="width">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.comment_status"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="onStatusChange(scope.row)"
              :disabled="scope.row.statusDisabled"
            >
            </el-switch>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="page"
        :page-sizes="[10, 20, 50]"
        :page-size.sync="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount"
        background
        :disabled="loading"
      />
    </el-card>
  </div>
</template>

<script>
import { getArticles, updateCommentStatus } from '@/api/article'
export default {
  name: 'CommentIndex',
  data() {
    return {
      articles: [],
      page: 1,
      pageSize: 10,
      totalCount: 0,
      loading: false
    }
  },
  created() {
    this.loadArticles()
  },
  methods: {
    loadArticles(page = 1) {
      this.loading = true
      this.page = page
      getArticles({
        page: page,
        per_page: this.pageSize,
        response_type: 'comment'
      }).then((res) => {
        this.loading = false
        const { results } = res.data.data
        results.forEach((article) => {
          article.statusDisabled = false
        })
        this.articles = results
        this.totalCount = res.data.data.total_count
      })
    },
    onStatusChange(row) {
      row.statusDisabled = true
      updateCommentStatus(row.id.toString(), row.comment_status).then((res) => {
        if (res.status !== 201) {
          this.$message.error('操作失败！')
          row.statusDisabled = false
        }
        this.$message.success(row.comment_status ? '开启成功！' : '关闭成功！')
        row.statusDisabled = false
      })
    },
    handleCurrentChange(newPage) {
      this.loadArticles(newPage)
    },
    handleSizeChange() {
      this.loadArticles(1)
    }
  }
}
</script>
<style lang="less" scoped>
.el-pagination {
  padding-top: 20px;
}
</style>
